package com.bw.user_module.login;

import android.text.TextUtils;

import androidx.databinding.ObservableField;
import androidx.room.Room;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.SPUtils;
import com.bw.base_application_sdk.BaseApplication;
import com.bw.common.entity.SaveEntity;
import com.bw.common.field.EventBusEntity;
import com.bw.common.mvvm.viewmodel.BaseViewModel;
import com.bw.common.utils.ToastShowUtils;
import com.bw.network_sdk.BaseEntity;
import com.bw.user_module.dao.UserDatabase;
import com.bw.user_module.user.UserEntity;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.schedulers.Schedulers;

public class LoginViewModel extends BaseViewModel<LoginRepository> {

    private String pushId = "";
    public ObservableField<String>mobile=new ObservableField<>();
    public ObservableField<String>pwd=new ObservableField<>();

    //用户登录方法
    public void login(){
        if (TextUtils.isEmpty(mobile.get())){
            ToastShowUtils.showShort("请输入手机号");
            return;
        }
        if (TextUtils.isEmpty(pwd.get())){
            ToastShowUtils.showShort("请输入密码");
            return;
        }
        //构建登录接口请求参数->发起接口请求
        Map<String,Object>map=new HashMap<>();
        map.put("mobile",mobile.get());
        map.put("pushId",pushId);
        map.put("pwd",pwd.get());
        respository.requestLogin(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this);
    }

    @Override
    public void onError(@NonNull Throwable e) {
        super.onError(e);
        LogUtils.e("onError"+e.getMessage());
    }

    @Override
    public void onResult(BaseEntity entity) {
        if (entity instanceof UserEntity){
            UserEntity userEntity = (UserEntity) entity;
            if (userEntity.data == null)
                return;
            SPUtils.getInstance().put("userid",userEntity.data.id);
            UserDatabase db = Room.databaseBuilder(BaseApplication.getInstance(),
                    UserDatabase.class, "user.db").build();
            respository.saveUser(userEntity,db)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(LoginViewModel.this);
            //用户信息保存本地room数据库中,
            //关闭界面
            //通知用户详情页刷新UI->UserFragment->未登录时->登录成功后，展示用户详细信息
            //页面间数据传递->数据刷新:UserFragment->onResume生命周期中获取本机缓存数据 ，
            //刷新UI状态,->由于当前UserFragment为用户全部信息展示入口->用户查看完订单后也会刷新用户信息
            //不足:每次都要刷新ui数据造成不必要操作以及性能浪费
            //优点:逻辑简单
            //EventBus->事件总线进行通知UserFragment进行刷新->用户本地信息状态发生改变后才刷新,
            //需要添加完整的注册以及解除注册以及传递数据过程
        }else if (entity instanceof SaveEntity){
            LogUtils.e("123"+entity.message);
            if (entity.message.equals("保存成功")){
                EventBusEntity eventBusEntity = new EventBusEntity();
                eventBusEntity.action = "refresh";
                EventBus.getDefault().post(eventBusEntity);
                EventBusEntity refreshCart = new EventBusEntity();
                refreshCart.action = "refreshCart";
                EventBus.getDefault().post(refreshCart);
                finishPage();
            }
        }

    }

    @Override
    protected void initData() {

    }

    //跳转注册方法
    public void register(){
        startPage("/user/registeractivity");
    }

    //跳转找回密码方法
    public void forget(){
        startPage("/user/forgetactivity");
    }
}
